package com.singfan.core.service.impl;

import com.singfan.common.util.StringUtil;
import com.singfan.core.dao.database.*;
import com.singfan.core.dao.database.model.*;
import com.singfan.core.dao.database.model.constant.BusinessStatusType;
import com.singfan.core.dao.database.model.constant.CollectType;
import com.singfan.core.dao.database.model.constant.GenderType;
import com.singfan.core.dao.database.model.constant.GlobalConfig;
import com.singfan.core.service.ICustomerService;
import com.singfan.core.util.AccessTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Author: HanSongHe
 * Date: 2015-07-29 1:12
 * Desc:
 */
@Service
public class CustomerServiceImpl implements ICustomerService {
    @Autowired
    private ICustomerDao customerDao;
    @Autowired
    private ICustomerTokenDao customerTokenDao;
    @Autowired
    private IPhoneSecurityCodeDao phoneSecurityCodeDao;
    @Autowired
    private ICustomerCollectDao customerCollectDao;
    @Autowired
    private IShopHairDao shopHairDao;
    @Autowired
    private IBarberDao barberDao;
    @Autowired
    private IBarberHairstyleDao barberHairstyleDao;
    @Autowired
    private ICustomerCouponDao customerCouponDao;
    @Autowired
    private ICouponRecordDao couponRecordDao;

    @Override
    public void createPhoneSecurityCode(String phoneNumber, String securityCode) {
        phoneSecurityCodeDao.createPhoneSecurityCode(phoneNumber, securityCode);
    }

    @Override
    public boolean verifyPhoneSecurityCode(String phoneNumber, String securityCode) {
        boolean result = false;
        PhoneSecurityCode phoneSecurityCode = phoneSecurityCodeDao.findPhoneSecurityCode(phoneNumber, securityCode);
        if (phoneSecurityCode != null){
            if (System.currentTimeMillis() - phoneSecurityCode.getCreateTime() <= GlobalConfig.PHONE_SECURITY_CODE_EXPIRED){
                result = true;
            }

            phoneSecurityCodeDao.deleteOne(phoneNumber, securityCode);
        }

        return result;
    }

    @Override
    public Customer createOrUpdateCustomer(String phoneNumber, String phoneType, int cityId) {
        Customer customer = customerDao.findCustomerByPhoneNumber(phoneNumber);
        if (customer != null){
            customerDao.updatePhoneTypeAndCityId(customer.getId(), phoneType, cityId);
        }
        else {
            customer = new Customer();
            customer.setNickName(phoneNumber);
            customer.setGender(GenderType.UNKNOWN);
            customer.setBirthday(null);
            customer.setStatus(BusinessStatusType.VERIFY_PASSED);
            customer.setPhoneNumber(phoneNumber);
            customer.setPhoneType(phoneType);
            customer.setCityId(cityId);
            customer.setHairStyleId(0);
            customer.setLikeHairStyle(0);
            customer.setCareer(null);
            customer.setPosition(null);
            customer.setAddress(null);
            customer.setCreateTime(new Date());

            customerDao.createCustomer(customer);
        }

        return customer;
    }

    @Override
    public String createAccessToken(long customerId, int cityId){
        String accessToken = AccessTokenUtil.createCustomerAccessToken(customerId);
        customerTokenDao.insertOrUpdateToken(customerId, accessToken, cityId);
        return accessToken;
    }

    @Override
    public Customer findCustomerByToken(String accessToken) {
        Customer customer = null;

        if (!StringUtil.isNullOrEmpty(accessToken)){
            CustomerToken customerToken = customerTokenDao.findCustomerToken(accessToken);
            if (customerToken != null){
                customer = customerDao.findCustomer(customerToken.getCustomerId());
            }
        }

        return customer;
    }

    @Override
    public CustomerToken findCustomerToken(String accessToken) {
        CustomerToken customerToken = null;
        if (!StringUtil.isNullOrEmpty(accessToken)){
            customerToken = customerTokenDao.findCustomerToken(accessToken);
        }
        return customerToken;
    }

    @Override
    public boolean isCollected(long customerId, long collectId, int collectType) {
        return customerCollectDao.isCollect(customerId, collectId, collectType);
    }

    @Override
    public void collect(long customerId, long collectId, int collectType) {
        customerCollectDao.collect(customerId, collectId, collectType);
        if (collectType == CollectType.SHOP){
            shopHairDao.updateCollectCount(collectId, true);
        }
        else if (collectType == CollectType.BARBER){
            barberDao.updateCollectCount(collectId, true);
        }
        else if (collectType == CollectType.BARBER_HAIR){
            barberHairstyleDao.updateCollectCount(collectId, true);
        }
    }

    @Override
    public void collectCancel(long customerId, long collectId, int collectType) {
        customerCollectDao.collectCancel(customerId, collectId, collectType);

        if (collectType == CollectType.SHOP){
            shopHairDao.updateCollectCount(collectId, false);
        }
        else if (collectType == CollectType.BARBER){
            barberDao.updateCollectCount(collectId, false);
        }
        else if (collectType == CollectType.BARBER_HAIR){
            barberHairstyleDao.updateCollectCount(collectId, false);
        }
    }

    @Override
    public List<CustomerCollect> findCollectList(long customerId, int collectType, int pageIndex, int pageSize) {
        return customerCollectDao.selectCollectList(customerId, collectType, pageIndex, pageSize);
    }

    @Override
    public void updateCustomer(Customer customer) {
        customerDao.update(customer);
    }

    @Override
    public List<CustomerCoupon> findCustomerCouponList(long customerId, boolean isExpired, int pageIndex, int pageSize) {
        return customerCouponDao.selectListByCustomerId(customerId, isExpired, pageIndex, pageSize);
    }

    @Override
    public CustomerCoupon findCustomerCoupon(long customerCouponId) {
        return customerCouponDao.selectById(customerCouponId);
    }

    @Override
    public void createCustomerCoupon(CustomerCoupon customerCoupon) {
        customerCouponDao.create(customerCoupon);
    }

    @Override
    public List<Long> findAllCustomerIdList() {
        return customerDao.selectAllCustomerIdList();
    }

    @Override
    public boolean isDisabledAccount(String phoneNumber) {
        return customerDao.isDisabledAccount(phoneNumber);
    }

    @Override
    public Customer findCustomerByPhoneNumber(String phoneNumber) {
        return customerDao.findCustomerByPhoneNumber(phoneNumber);
    }

    @Override
    public void updateCustomerStatus(long customerId, int status) {
        customerDao.updateCustomerStatus(customerId, status);
    }

    @Override
    public void deleteCustomerToken(long customerId) {
        customerTokenDao.delete(customerId);
    }

    @Override
    public void createCoupon(CouponRecord couponRecord) {
        couponRecordDao.create(couponRecord);

        List<Long> customerIdList = customerDao.selectAllCustomerIdList();
        for (Long customerId : customerIdList){
            CustomerCoupon customerCoupon = new CustomerCoupon();
            customerCoupon.setCouponRecordId(couponRecord.getId());
            customerCoupon.setCustomerId(customerId);
            customerCoupon.setAmount(couponRecord.getAmount());
            customerCoupon.setPriceCondition(couponRecord.getPriceCondition());
            customerCoupon.setShopId(couponRecord.getShopId());
            customerCoupon.setShopMasterName(couponRecord.getShopMasterName());
            customerCoupon.setShopSlaveName(couponRecord.getShopSlaveName());
            customerCoupon.setExpiredTime(couponRecord.getExpiredTime());
            customerCoupon.setCreateTime(couponRecord.getBeginTime());

            customerCouponDao.create(customerCoupon);
        }
    }

    @Override
    public List<CouponRecord> findAllCouponRecordList(){
        return couponRecordDao.selectAllCouponRecordList();
    }

    @Override
    public void deleteCoupon(long couponId) {
        customerCouponDao.deleteByCouponId(couponId);
        couponRecordDao.delete(couponId);
    }
}
